package repository

import (
	"context"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/jmoiron/sqlx"
	"go.uber.org/zap"
)

// MysqlDirectMethodRepository is data/repository implementation
// of service layer EmployeeRepository
type MysqlDirectMethodRepository struct {
	DB *sqlx.DB
}

// NewDirectMethodRepository is a factory for initializing Employee Repository
func NewDirectMethodRepository(db *sqlx.DB) model.DirectMethodRepository {
	return &MysqlDirectMethodRepository{
		DB: db,
	}
}

func (m *MysqlDirectMethodRepository) Create(ctx context.Context, d *model.DirectMethod) error {
	query := `INSERT INTO direct_method(payment_method_id, bank, account) VALUES(?, ?, ?)`

	if _, err := m.DB.Exec(query, d.PaymentMethodID, d.Bank, d.Account); err != nil {
		zap.L().Error("repository cound not create a direct method with err: ", zap.Error(err))
		return apperrors.NewInternal()
	}

	return nil
}

func (m *MysqlDirectMethodRepository) Update(ctx context.Context, d *model.DirectMethod) error {
	query := `UPDATE direct_method SET bank = ?, account = ? WHERE payment_method_id = ?`

	if _, err := m.DB.Exec(query, d.Bank, d.Account, d.PaymentMethodID); err != nil {
		zap.L().Error("repository cound not update a direct method with err: ", zap.Error(err))
		return err
	}

	return nil
}
